package.cpath = "luaclib/?.so"
local crypt = require "client.crypt"

local clientkey = "11111111" --8byte random 
print("clientkey:" , clientkey)
local ckey = crypt.dhexchange(clientkey)
print("ckey:\t" , crypt.hexencode(ckey))

local serverkey = "22222222"
print("serverkey:" , serverkey)
local skey = crypt.dhexchange(serverkey)
print("skey:\t" , crypt.hexencode(skey))

local csecret = crypt.dhsecret(skey, clientkey)
print("use skey clientkey dhsecret:", crypt.hexencode(csecret)) --交换成功

local ssecret = crypt.dhsecret(ckey, serverkey)
print("use ckey serverkey dhsecret:", crypt.hexencode(ssecret)) --交换成功

local ssecret = crypt.dhsecret(ckey, skey)                  --交换失败
print("use ckey skey dhsecret:\t", crypt.hexencode(ssecret))
-- 客户端使用服务端公钥(skey)和自己的私钥(clientkey)计算共享密钥
-- 服务端使用客户端公钥(ckey)和自己的私钥(serverkey)计算共享密钥
-- 这两个操作应该产生相同的共享密钥，证明密钥交换成功